<template>
  <div>
    <el-dialog
      title="提示"
      :visible.sync="visible"
      :modal-append-to-body="false"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      @close="closeDialog"
      width="500px"
    >
      <p>请确认是否提交单号: {{form.ref}} 的采购入库单?</p>
      <div slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="Submit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    form: {
      style: Object
    }
  },
  data() {
    return {
      submit: {},
      list: [],
      visible: true
    };
  },
  methods: {
    Submit() {
      window.console.log(this.form);
      this.form.statu = "已入库";
      let date = new Date();
      let pandian = {
        id: "",
        typename: "采购盘点",
        storeid: this.form.storeid,
        returnRef: "/",
        checkRef: "/",
        calRef: this.form.ref,
        date:
          date.getFullYear() +
          "-" +
          (date.getMonth() + 1) +
          "-" +
          date.getDate(),
        bizname: this.form.bizusername,
        memo: "",
        statu: "待盘点",
        storename: this.form.storename
      };
      date.setTime(new Date().getTime());
      pandian.checkRef =
        "CO" +
        date.getFullYear() +
        (date.getMonth() + 1) +
        date.getDate() +
        date.getHours() +
        date.getMinutes() +
        date.getSeconds();
      let pandianlist = [];

      this.$http
        .post("orderdetail/somelist", { orderid: this.form.orderid })
        .then(response => {
          this.list = response.data;
          this.list.forEach(Row => {
            pandianlist.push({
              id: "",
              code: Row.code,
              name: Row.name,
              xinghao: Row.spec,
              count: Row.count,
              unitname: Row.unitname,
              memo: "",
              checkId: "",
              spec: Row.spec
            });
          });
          this.$http
            .post("/purchaseenterlist/update", this.form)
            .then(response => {
              if (response.data == "success") {
                this.$http
                  .post("order/finish", { id: this.form.orderid })
                  .then(response => {
                    if (response.data == "success") {
                      //提交盘点
                      this.$http
                        .post("assetCheck/insert", pandian)
                        .then(response => {
                          pandianlist.forEach(Row => {
                            Row.checkId = response.data;
                          });
                          this.$http
                            .post("assetCheckDetail/insert", pandianlist)
                            .then(response => {
                              if (response.data == "success") {
                                this.$message({
                                  message: "提交完成",
                                  type: "success"
                                });
                                this.NeedRefresh();
                                this.visible = false;
                              }
                            });
                        });
                    }
                  });
              }
            });
        });
    },
    closeDialog() {
      this.$emit("dialog-close");
    },
    NeedRefresh() {
      this.$emit("refresh");
    }
  }
};
</script>

<style>
</style>